<template>
    <div class="selectpp-auditor">
        <div style="text-align:center;margin-bottom:10px">
            <RadioGroup v-model="partype" type="button" size="small" @on-change="ptchange">
                <Radio label="dus">职务</Radio>
                <Radio label="dep">部门</Radio>
                <Radio label="all">所有人</Radio>
            </RadioGroup>
        </div>
        <Input v-model="seartxt" size="small" placeholder="搜索人员"  @on-change="toSearch(seartxt)" v-if="partype=='all'"/>
        <Select v-model="dutie_id" v-if="partype=='dus'" size="small" @on-change="chax" placeholder="请选择职务">
            <Option :value="item.value" v-for="(item,ind) in duties" :key="ind">{{item.title}}</Option>
        </Select>
        <Cascader :data="trees" size="small" @on-change="changed" :load-data="loadData" change-on-select placeholder="请选择部门" v-if="partype=='dep'"></Cascader>
        <div style="min-height:250px;margin-top:7px;height:400px;overflow:auto">
            <RadioGroup v-model="examine_user" @on-change="toselec">
                <div v-for="item in examiners" :key="item.user_id" style="margin-bottom:2px"><Radio :label="item.user_id">{{item.teacher_name!=null?(item.teacher_name.zh||item.teacher_name.mn):''}}</Radio></div>
            </RadioGroup>
        </div>
    </div>
</template>

<script>

export default {
    data() {
        return {
            partype: 'dus',
            dutie_id: '',
            seartxt: '',
            examine_user: '',
            duties: [],
            examiners: [],
            examiners_copy: [],
            // 菜单树
            trees: []
        }
    },
    props: ['value'],
    methods: {
        ptchange(type) {
            if(type=='all') {
                this.examiners = this.examiners_copy
        
			} else if(type=='dus'){
				this.examiners = []
				this.dutie_id = ''
			} else if(type=='dep') {
                this.examiners = []
                if(this.trees.length==0) {
                    this.loadep()
                }
            }
        },
        chax(id) {
            let url = `api/user/v1/get/teacher/user/list?duties_id=[${id}]`
			this.$http.get(url).then(res=>{
				this.examiners = res.data
			}).catch(
				err => this.$Message.error('人员获取失败！')
			)
        },
        // 搜索人员
		toSearch(txt) {
			if(txt.length!=0) {
				this.examiners = this.examiners_copy.filter(item=> {
					return new RegExp(txt).test(item.teacher_name.zh)
				})
			} else {
				this.examiners = this.examiners_copy
			}
		},
        zwload() {
			this.$http.get('api/user/v1/get/duties/list').then(res=> {
				this.duties = res.data.map(item=> ({
                    value: item.id,
                    title: item.duties_name.zh?item.duties_name.zh:item.duties_name.mn
                }))
			}).catch(
				err => this.$Message.error('职务加载失败！')
			)
        },
        getExamine(){
			this.$http.get('api/user/v1/get/teacher/user/list').then(val=>{
				this.examiners = []
				this.examiners_copy = val.data
			}).catch(
				err => console.log(err)
			)
        },
        // 加载部门
        loadep() {
           this.$http.get('api/user/v1/department/info').then(res=> {
                this.trees = this.process(res.data)
           }).catch(
                err => this.$Message.error('部门加载失败！')
            )
        },
        // 处理数据
        process(data) {
            let arr = []
            data.forEach(item => {
                item.title = item.label = item.department_name.zh?item.department_name.zh:item.department_name.mn
                item.value = item.id
                if(item.childs) {
                    item.loading = false,
                    item.children = []
                }
                arr.push(item)
            })
            return arr
        },
        // 异步加载二级部门
        loadData(item, callback) {
            item.loading = true;
            this.$http.get('api/user/v1/department/info?pid='+item.id).then(res=> {
                item.children = this.process(res.data)
                item.loading = false;
                callback()
            }).catch(
                err => this.$Message.error('子部门加载失败！')
            )
        },
        changed(value) {
            let id = value[value.length-1]
            let url = `api/user/v1/get/teacher/user/list?department_id=[${id}]`
			this.$http.get(url).then(res=>{
				this.examiners = res.data
			}).catch(
				err => this.$Message.error('人员获取失败！')
			)
        },
        toselec(tp) {
            this.$emit('on-select',tp)
            console.log(tp)
        }
    },
    created() {
        this.zwload()
        this.getExamine()
    }
}
</script>

<style>

</style>
